package com.ptswitch.material.model.material;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.NotEmpty;
import org.hxzon.project.BaseEntity;

import com.ptswitch.material.model.contract.ContractItem;

@Entity
//@Indexed
public class Material extends BaseEntity implements Comparable<Material> {

    private static final long serialVersionUID = 5156788179539859892L;

    public static final String State_UnPublish = "未发布";
    public static final String State_Recommend = "推荐";
    public static final String State_UnRecommend = "不推荐";
    public static final String State_Normal = "一般";
    public static final String State_StopUse = "停用";
    public static final String IndustryLevel_Industry = "工业级";
    public static final String IndustryLevel_Commercial = "商业级";

    @OneToMany(mappedBy = "material", fetch = FetchType.LAZY)
    // @JoinColumn(name ="code",referencedColumnName="code" )
    private List<BomMaterial> bomMaterials;

    @OneToMany(mappedBy = "material", fetch = FetchType.LAZY)
    @OrderBy("code desc")
    private List<ContractItem> contractItems;

    @ManyToOne(fetch = FetchType.EAGER)
    @NotFound(action = NotFoundAction.IGNORE)
    private MaterialOrigFile origFile;

    @OneToMany(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "ownerId", referencedColumnName = "id")
    private List<MaterialAttachment> attachments;

    @Column(columnDefinition = DefineVarchar32)
    @Size(max = DefineSize32)
    @NotEmpty
//	@Field(name="search")
    private String code;// 物料编码

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
    @NotEmpty
//	@Field(name="search")
    private String name;// 物料名称

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String specification;// 规格型号

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String footprint;// 器件封装

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String brand;// 品牌

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String description;// 器件描述

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String supplier;// 供货商

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String pcbDecal;// pcb封装

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String remark;// 备注

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@Field(name="search")
    private String state = State_UnPublish;// 采购状态

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//  @Field(name="search")
    private String industryLevel = IndustryLevel_Commercial;// 使用级别

    public List<ContractItem> getContractItems() {
        return contractItems;
    }

    public void setContractItems(List<ContractItem> contractItems) {
        this.contractItems = contractItems;
    }

    public List<BomMaterial> getBomMaterials() {
        return bomMaterials;
    }

    public void setBomMaterials(List<BomMaterial> bomMaterials) {
        this.bomMaterials = bomMaterials;
    }

    public MaterialOrigFile getOrigFile() {
        return origFile;
    }

    public void setOrigFile(MaterialOrigFile origFile) {
        this.origFile = origFile;
    }

    public List<MaterialAttachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(List<MaterialAttachment> attachments) {
        this.attachments = attachments;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpecification() {
        return specification;
    }

    public void setSpecification(String specification) {
        this.specification = specification;
    }

    public String getFootprint() {
        return footprint;
    }

    public void setFootprint(String footprint) {
        this.footprint = footprint;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSupplier() {
        return supplier;
    }

    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }

    public String getPcbDecal() {
        return pcbDecal;
    }

    public void setPcbDecal(String pcbDecal) {
        this.pcbDecal = pcbDecal;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getIndustryLevel() {
        return industryLevel;
    }

    public void setIndustryLevel(String industryLevel) {
        this.industryLevel = industryLevel;
    }

    public String toString() {
        return this.code + "[" + this.name + "]" + this.description + ";" + this.specification + ";采购状态：" + this.state;
    }

    public String print() {
        StringBuilder sb = new StringBuilder();
        sb.append("code:").append(code).append(",name:").append(name).append(",spec:").append(specification).append(",footprint:").append(footprint).append(",brand:").append(brand).append(",desc:")
                .append(description).append(",supplier:").append(supplier).append(",pcbDecal:").append(pcbDecal).append(",remark:").append(remark);
        return sb.toString();
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Material)) {
            return false;
        }
        Material other = (Material) o;
        return new EqualsBuilder().append(this.code, other.getCode()).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(this.code).toHashCode();
    }

    public int compareTo(Material o) {
        return this.code.compareTo(o.getCode());
    }
}
